package com.ethink.platform.controller.order;

import com.ethink.framework.common.web.BaseController;
import com.ethink.framework.common.web.context.SPRequestContextHolder;
import com.ethink.framework.common.web.response.CommonResponse;
import com.ethink.order.api.TocSaleReturnImportLogFacade;
import com.ethink.order.api.TocSaleReturnImportLogResultFacade;
import com.ethink.order.api.info.TocSaleReturnImportLogResultInfo;
import com.ethink.order.api.model.TocSaleReturnImportModel;
import com.ethink.order.api.request.TocSaleReturnImportLogAddRequest;
import com.ethink.order.api.request.TocSaleReturnImportLogQueryRequest;
import com.ethink.order.api.request.UpdateStatusBaseRequest;
import com.ethink.order.api.response.TocSaleReturnImportLogDetailResponse;
import com.ethink.platform.controller.order.info.TocSaleReturnImportDetailLogInfo;
import com.ethink.platform.manager.TocSaleReturnImportManager;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * TOC销售退单导入日志 Controller
 *
 * @author default
 * @date 2022-12-16
 */
@Tag(name = "TocSaleReturnImportLogController", description = "TOC销售退单导入日志 Controller")
@RestController
@Slf4j
@RequestMapping("platform-server/v1/order-service")
public class TocSaleReturnImportLogController extends BaseController {

    @Autowired
    private TocSaleReturnImportLogFacade tocSaleReturnImportLogFacade;

    @Autowired
    private TocSaleReturnImportManager tocSaleReturnImportManager;

    @Autowired
    private TocSaleReturnImportLogResultFacade tocSaleReturnImportLogResultFacade;


    @Operation(summary = "新增")
    @PostMapping("/tocSaleReturnImportLog/add")
    public CommonResponse addTocSaleReturnImportLog(@RequestBody TocSaleReturnImportLogAddRequest request) {
        request.setCreateUserCode(SPRequestContextHolder.getCurrentSystemUserLoginName());
        request.setCreateUserName(SPRequestContextHolder.getCurrentSystemUserRealName());
        tocSaleReturnImportLogFacade.add(request);
        return succeed();
    }


    @Operation(summary = "导入")
    @PostMapping("/tocSaleReturnImportLog/import")
    public CommonResponse<List<TocSaleReturnImportDetailLogInfo>> importTocSaleReturnImportLog(@RequestParam("file") MultipartFile file
            ,@RequestParam("saleSubChannel") String saleSubChannel) {
        List<TocSaleReturnImportDetailLogInfo> tocSaleReturnImportDetailLogInfos = tocSaleReturnImportManager.importTocSaleReturnLog(file, saleSubChannel);
        return succeedWith(tocSaleReturnImportDetailLogInfos);
    }

    @Operation(summary = "分页查询")
    @GetMapping("/tocSaleReturnImportLog/pageQuery")
    public CommonResponse<List<TocSaleReturnImportModel>> queryTocSaleReturnImportLog(TocSaleReturnImportLogQueryRequest request) {
        return succeedWithPage(tocSaleReturnImportLogFacade.pageQuery(request));
    }


    @Operation(summary = "删除")
    @DeleteMapping("/tocSaleReturnImportLog/delete/{id}")
    public CommonResponse deleteTocSaleReturnImportLog(@PathVariable Long id) {
        tocSaleReturnImportLogFacade.delete(id);
        return succeed();
    }

    @Operation(summary = "详情")
    @GetMapping("/tocSaleReturnImportLog/detail/{id}")
    public CommonResponse<TocSaleReturnImportLogDetailResponse> getTocSaleReturnImportLogById(@PathVariable("id") Long id) {
        TocSaleReturnImportLogDetailResponse response = tocSaleReturnImportLogFacade.queryDetail(id);
        return succeedWith(response);
    }


    @Operation(summary = "修改订单状态")
    @PutMapping("/tocSaleReturnImportLog/updateStatus")
    public CommonResponse updateTocSaleReturnImportLogOrderStatus(@RequestBody @Validated UpdateStatusBaseRequest request) {
     /*   if (TocSaleReturnImportLogOrderStatusEnum.S2.eq(request.getOrderStatus())) {
            tocSaleReturnImportLogResultFacade.add(request.getId());
        }*/
        tocSaleReturnImportLogFacade.updateStatus(request);
        return succeed();
    }

    @Operation(summary = "退单结果查询")
    @GetMapping("/tocSaleReturnImportLog/result/{id}")
    public CommonResponse<List<TocSaleReturnImportLogResultInfo>> getTocSaleReturnLogResult(@PathVariable("id") Long id) {
        List<TocSaleReturnImportLogResultInfo> resultInfos = tocSaleReturnImportLogResultFacade.queryListByLogId(id);
        return succeedWith(resultInfos);
    }

}